{% if payment_pp_express_incontext_disable != 1 %}
<div class="buttons">
  <div class="pull-right">
    <a id="button-confirm" href="{{ continue }}"></a>
    <a id="button-confirms" >sss</a>
  </div>
</div>
<script type="text/javascript"><!--
//$('#button-confirms').on('click', function() {
{% if not logged %}
document.querySelector('#button-confirms').addEventListener('click', function(event) {
    $.ajax({
        url: 'index.php?route=checkout/guest/savehebing',
        type: 'post',
        data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select'),
        dataType: 'json',
        async:false,
        success: function(json) {
            
            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                var elementnoteo='';
				for (i in json['error']) {
					var element = $('#input-payment-' + i.replace('_', '-'));
                    if(!elementnoteo){
					   elementnoteo=element;
					}
					if ($(element).parent().hasClass('input-group')) {
						$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
					} else {
						$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
					}
				}
				//console.log(element.attr("id"));
                window.location.hash = "#"+elementnoteo.attr("id");
				// Highlight any found errors
				$('.text-danger').parent().addClass('has-error');
            } else {
                
                var shoppingsameaddress = $('#collapse-payment-address input[name=\'shoppingsameaddress\']:checked').val();
				if(shoppingsameaddress=='shoppingdifferent'){
				    savegshoping();  
				}else{
				   savegpaddess();
				}
                 
                
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
	
});
{% else %}

        

document.querySelector('#button-confirms').addEventListener('click', function(event) {
		var select=$('#collapse-payment-address select').val();
		var shoppingsameaddress = $('#collapse-payment-address input[name=\'shoppingsameaddress\']:checked').val();
				//if(shoppingsameaddress=='shoppingdifferent'){
		//alert(select);return;
		if(select=='new'&&shoppingsameaddress=='shopping'){
		      $.ajax({
					url: 'index.php?route=checkout/payment_address/savehebing',
					type: 'post',
					data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'password\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select'),
					dataType: 'json',
					beforeSend: function() {
						$('#button-payment-address').button('loading');
					},
					complete: function() {
						$('#button-payment-address').button('reset');
					},
					async:false, 
					success: function(json) {
						$('.alert-dismissible, .text-danger').remove();
						$('.form-group').removeClass('has-error');

						if (json['redirect']) {
							location = json['redirect'];
						} else if (json['error']) {
							if (json['error']['warning']) {
								$('#collapse-payment-address .panel-body').prepend('<div class="alert alert-warning alert-dismissible">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
							}

							for (i in json['error']) {
								var element = $('#input-payment-' + i.replace('_', '-'));

								if ($(element).parent().hasClass('input-group')) {
									$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
								} else {
									$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
								}
							}

							// Highlight any found errors
							$('.text-danger').parent().parent().addClass('has-error');
						} else {
							   
							$.ajax({
								url: 'index.php?route=checkout/confirm_he',
								dataType: 'html',
								async:false, 
								success: function(html) {
									
									var e = document.createEvent("MouseEvents");
									 e.initEvent("click", true, true);
									document.getElementById("button-confirm").dispatchEvent(e);
								},
								error: function(xhr, ajaxOptions, thrownError) {
									alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
								}
							});   
							   
							   
						}
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
		}else if(select=='new'&&shoppingsameaddress=='shoppingdifferent'){
		
		    $.ajax({
					url: 'index.php?route=checkout/payment_address/savehebing',
					type: 'post',
					data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'password\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select'),
					dataType: 'json',
					beforeSend: function() {
						$('#button-payment-address').button('loading');
					},
					complete: function() {
						$('#button-payment-address').button('reset');
					},
					async:false, 
					success: function(json) {
						$('.alert-dismissible, .text-danger').remove();
						$('.form-group').removeClass('has-error');

						if (json['redirect']) {
							location = json['redirect'];
						} else if (json['error']) {
							if (json['error']['warning']) {
								$('#collapse-payment-address .panel-body').prepend('<div class="alert alert-warning alert-dismissible">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
							}

							for (i in json['error']) {
								var element = $('#input-payment-' + i.replace('_', '-'));

								if ($(element).parent().hasClass('input-group')) {
									$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
								} else {
									$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
								}
							}

							// Highlight any found errors
							$('.text-danger').parent().parent().addClass('has-error');
						} else {
							$.ajax({
								url: 'index.php?route=checkout/shipping_address/hebingsave',
								type: 'post',
								data: $('#collapse-shipping-address input[type=\'text\'], #collapse-shipping-address input[type=\'date\'], #collapse-shipping-address input[type=\'datetime-local\'], #collapse-shipping-address input[type=\'time\'], #collapse-shipping-address input[type=\'password\'], #collapse-shipping-address input[type=\'checkbox\']:checked, #collapse-shipping-address input[type=\'radio\']:checked, #collapse-shipping-address textarea, #collapse-shipping-address select'),
								dataType: 'json',
								beforeSend: function() {
									$('#button-shipping-address').button('loading');
								},
								success: function(json) {
									$('.alert-dismissible, .text-danger').remove();
									$('.form-group').removeClass('has-error');

									if (json['redirect']) {
										location = json['redirect'];
									} else if (json['error']) {
										$('#button-shipping-address').button('reset');

										if (json['error']['warning']) {
											$('#collapse-shipping-address .panel-body').prepend('<div class="alert alert-warning alert-dismissible">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
										}

										for (i in json['error']) {
											var element = $('#input-shipping-' + i.replace('_', '-'));

											if ($(element).parent().hasClass('input-group')) {
												$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
											} else {
												$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
											}
										}

										// Highlight any found errors
										$('.text-danger').parent().parent().addClass('has-error');
									} else {
										$.ajax({
											url: 'index.php?route=checkout/confirm_he',
											dataType: 'html',
											async:false, 
											success: function(html) {
												
												var e = document.createEvent("MouseEvents");
												 e.initEvent("click", true, true);
												document.getElementById("button-confirm").dispatchEvent(e);
											},
											error: function(xhr, ajaxOptions, thrownError) {
												alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
											}
										});
									}
								},
								error: function(xhr, ajaxOptions, thrownError) {
									alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
								}
							});
						}
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
		    
		
		
		}else if(select!='new'&&shoppingsameaddress=='shopping'){
		    $.ajax({
				url: 'index.php?route=checkout/confirm_he',
				dataType: 'html',
				async:false, 
				success: function(html) {
					
					var e = document.createEvent("MouseEvents");
					 e.initEvent("click", true, true);
					document.getElementById("button-confirm").dispatchEvent(e);
				},
				error: function(xhr, ajaxOptions, thrownError) {
					alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		    		

		
		}else if(select!='new'&&shoppingsameaddress=='shoppingdifferent'){
		    $.ajax({
				url: 'index.php?route=checkout/shipping_address/hebingsave',
				type: 'post',
				data: $('#collapse-shipping-address input[type=\'text\'], #collapse-shipping-address input[type=\'date\'], #collapse-shipping-address input[type=\'datetime-local\'], #collapse-shipping-address input[type=\'time\'], #collapse-shipping-address input[type=\'password\'], #collapse-shipping-address input[type=\'checkbox\']:checked, #collapse-shipping-address input[type=\'radio\']:checked, #collapse-shipping-address textarea, #collapse-shipping-address select'),
				dataType: 'json',
				beforeSend: function() {
					$('#button-shipping-address').button('loading');
				},
				success: function(json) {
					$('.alert-dismissible, .text-danger').remove();
					$('.form-group').removeClass('has-error');

					if (json['redirect']) {
						location = json['redirect'];
					} else if (json['error']) {
						$('#button-shipping-address').button('reset');

						if (json['error']['warning']) {
							$('#collapse-shipping-address .panel-body').prepend('<div class="alert alert-warning alert-dismissible">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
						}

						for (i in json['error']) {
							var element = $('#input-shipping-' + i.replace('_', '-'));

							if ($(element).parent().hasClass('input-group')) {
								$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
							} else {
								$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
							}
						}

						// Highlight any found errors
						$('.text-danger').parent().parent().addClass('has-error');
					} else {
						$.ajax({
							url: 'index.php?route=checkout/confirm_he',
							dataType: 'html',
							async:false, 
							success: function(html) {
								
								var e = document.createEvent("MouseEvents");
								 e.initEvent("click", true, true);
								document.getElementById("button-confirm").dispatchEvent(e);
							},
							error: function(xhr, ajaxOptions, thrownError) {
								alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
							}
						});
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		    		

		
		}
        


      
    });
{% endif %}

function savegpaddess(){
    ///////
							$.ajax({
								url: 'index.php?route=checkout/confirm_he',
								dataType: 'html',
								async:false, 
								success: function(html) {
									
									var e = document.createEvent("MouseEvents");
									 e.initEvent("click", true, true);
									document.getElementById("button-confirm").dispatchEvent(e);
							   
									
									//window.location = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-25T76600JU658143L&useraction=commit";

								},
								error: function(xhr, ajaxOptions, thrownError) {
									alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
								}
							});
							///////
}
function savegshoping(){
    $.ajax({
        url: 'index.php?route=checkout/guest_shipping/savehebing',
        type: 'post',
        data: $('#collapse-shipping-address input[type=\'text\'], #collapse-shipping-address input[type=\'date\'], #collapse-shipping-address input[type=\'datetime-local\'], #collapse-shipping-address input[type=\'time\'], #collapse-shipping-address input[type=\'password\'], #collapse-shipping-address input[type=\'checkbox\']:checked, #collapse-shipping-address input[type=\'radio\']:checked, #collapse-shipping-address textarea, #collapse-shipping-address select'),
        dataType: 'json',
        beforeSend: function() {
        	$('#button-guest-shipping').button('loading');
		},
		async:false,
        success: function(json) {
            $('.alert-dismissible, .text-danger').remove();
			$('.form-group').removeClass('has-error');

            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                $('#button-guest-shipping').button('reset');

                if (json['error']['warning']) {
                    $('#collapse-shipping-address .panel-body').prepend('<div class="alert alert-danger alert-dismissible">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }
                var elementnote='';
				for (i in json['error']) {
					var element = $('#input-shipping-' + i.replace('_', '-'));
                    if(!elementnote){
					   elementnote=element;
					}
					if ($(element).parent().hasClass('input-group')) {
						$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
					} else {
						$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
					}
				}
                window.location.hash = "#"+elementnote.attr("id");
				// Highlight any found errors
				$('.text-danger').parent().addClass('has-error');
            } else {
			    savegpaddess();
                
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });


}




window.paypalCheckoutReady = function () {
  paypal.checkout.setup('{{ username }}', {
    container: 'button-confirm',
    environment: 'sandbox'
  });
};
//--></script>
<script src="catalog/view/javascript/paypal.js" async></script>
{% else %}
<div class="buttons">
  <div class="pull-right">
    <a href="{{ continue }}" class="btn btn-primary" id="button-confirm" data-loading-text="{{ text_loading }}">{{ button_continue }}</a>
  </div>
</div>
<script type="text/javascript"><!--
    $('#button-confirm').on('click', function() {
        $('#button-confirm').button('loading');
    });
//--></script>
{% endif %}
